﻿using ServerBase.Config;

namespace SvGame.Logic;

public static partial class GameLogic
{
    public static void ActionDebug(this IRole me)
    {
        loger.Debug($"ActEquipWeaponType {me.Comb.ActEquipWeaponType}");
        loger.Debug($"ActEquipUsageType {me.Comb.ActEquipUsageType}");
        loger.Debug($"ActEquipWeapon {me.Comb.ActEquipWeaponName}");
        loger.Debug($"ActList {me.Comb.ActList.Count}");
    }
    public static void ActionSet(this IRole me)
    {
        // 重置
        me.Comb.Weapon = null;

        //攻击动作
        {
            //是否携带武器
            {
                me.Comb.ActEquipWeaponType = EWeaponType.无;
                me.Comb.ActEquipUsageType = EUsageType.扑击格斗;
                if (me.DictWear.TryGetValue(EEquipType.武器, out var uid))
                {
                    if (me.DictEquip.TryGetValue(uid, out var equip))
                    {
                        me.Comb.Weapon = equip;
                        me.Comb.ActEquipWeaponType = (EWeaponType)equip.Cfg.WeaponType;
                        me.Comb.ActEquipUsageType = ConfWeaponTypeExt[me.Comb.ActEquipWeaponType].UsageType;
                        me.Comb.ActEquipWeaponName = equip.Cfg.Name;
                    }
                }
            }

            me.Comb.ActList = new List<ConfigCbtAction>();
            //技能action
            if (me.UsageGet(me.Comb.ActEquipUsageType, out var skillid, out _))
            {
                if (ConfSkillActionNormal.TryGetValue(skillid, out var skillactions))
                {
                    me.Comb.ActList = skillactions;
                }
                else
                {
                    loger.Warn($"找不到技能{skillid} Action");
                    //种族默认action
                    me.Comb.ActList = ConfRaceExt[me.Race].ListActions;
                }
            }
            else
            {
                if (me.Comb.ActEquipUsageType == EUsageType.扑击格斗)
                {
                    //种族默认action
                    me.Comb.ActList = ConfRaceExt[me.Race].ListActions;
                }
                else
                {
                    me.Comb.ActList = ConfWeaponTypeExt[me.Comb.ActEquipWeaponType].ListActions;
                }
            }
            if (me.Comb.ActList.IsNullOrEmpty())
            {
                //种族默认action
                me.Comb.ActList = ConfRaceExt[me.Race].ListActions;
            }
        }

        //招架描述列表
        {
            me.Comb.ActParryMsgs = new List<string>();

            if (me.UsageGet(EUsageType.拆招卸力, out var victimskillparry, out var _))
            {
                if (ConfSkill.TryGetValue(victimskillparry, out var confskillparry))
                {
                    if ((EWeaponType)(confskillparry.WeaponType) == me.Comb.ActEquipWeaponType)
                    {
                        if (ConfSkillParryExt.TryGetValue(victimskillparry, out var msgsparry))
                        {
                            me.Comb.ActParryMsgs = msgsparry;
                        }
                    }
                }
            }
            if (me.Comb.ActParryMsgs.Count <= 0)
            {
                // 带武器
                if (me.Comb.Weapon == null)
                {
                    foreach (var item in ConfDescParryU.Values)
                    {
                        me.Comb.ActParryMsgs.Add(item.Desc);
                    }
                }
                else
                {
                    foreach (var item in ConfDescParryW.Values)
                    {
                        me.Comb.ActParryMsgs.Add(item.Desc);
                    }
                }
            }
        }

        //调试
        ActionDebug(me);
    }
    public static ConfigCbtAction ActionGet(this IRole me)
    {
        if (!me.Comb.PerformActName.IsNullOrEmpty())
        {
            if (ConfCbtAction.TryGetValue(me.Comb.PerformActName, out var act))
            {
                return act;
            }
            else
            {
                loger.Error($"ActionGet PerformActName error {me.Comb.PerformActName}");
            }
        }
        if (me.Comb.ActList.Count <= 0)
        {
            loger.Error($"ActionGet bad action ActList 0 uid:{me.Uid} name{me.Name}");
            return null;
        }
        return me.Comb.ActList.RandomSingle();
    }
}
